package br.com.digitalizar.relproducao.bean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.QName;

import br.com.digitalizar.relproducao.core.Digger;

/**
 * 
 * @author Rafael de Morais
 * 
 */
public class RelProducaoBean {

	private static final long serialVersionUID = 9079123687831081205L;

	protected SearchService searchService;

	private NodeService nodeService;

	private DictionaryService dictionaryService;

	protected Date startDate = Calendar.getInstance().getTime();
	protected Date endDate = Calendar.getInstance().getTime();

	protected Digger digger;

	public void selectedDateHandler(ActionEvent evt) {
		performSearch();
	}

	private void performSearch() {
		digger = new Digger(searchService, nodeService, dictionaryService);

		digger.setRange(startDate, endDate);
		digger.setContentTypes(getTypes());

		digger.performSearch();		
	}

	public List<ReportBean> getReports() {
		if (digger == null) {
			return new ArrayList<ReportBean>();
		}
		return digger.getReportBeans();
	}
	
	public void all(ActionEvent evt) {
		Calendar startCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();
		
		startCal.set(Calendar.DAY_OF_YEAR, startCal.getGreatestMinimum(Calendar.DAY_OF_YEAR));
		toDayLimite(startCal, true);
		startDate = startCal.getTime();
		
		endCal.set(Calendar.DAY_OF_YEAR, endCal.getActualMaximum(Calendar.DAY_OF_YEAR));
		toDayLimite(endCal, false);
		endDate = endCal.getTime();
		
		performSearch();
	}
	
	public void today(ActionEvent evt) {
		Calendar startCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();
		
		toDayLimite(startCal, true);
		startDate = startCal.getTime();
		
		toDayLimite(endCal, false);
		endDate = endCal.getTime();
		
		performSearch();
	}
	
	public void last7Days(ActionEvent evt) {
		Calendar startCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();
		
		lastXDays(startCal, 7);
		toDayLimite(startCal, true);
		startDate = startCal.getTime();
		
		toDayLimite(endCal, false);
		endDate = endCal.getTime();
		
		performSearch();
		
	}
	
	public void last30Days(ActionEvent evt) {
		Calendar startCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();
		
		lastXDays(startCal, 30);
		toDayLimite(startCal, true);
		startDate = startCal.getTime();
		
		toDayLimite(endCal, false);
		endDate = endCal.getTime();
		
		performSearch();
	}
	
	public void thisMonth(ActionEvent evt) {
		Calendar startCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();
		
		startCal.set(Calendar.DAY_OF_MONTH, startCal.getGreatestMinimum(Calendar.DAY_OF_MONTH));
		toDayLimite(startCal, true);
		startDate = startCal.getTime();
		
		endCal.set(Calendar.DAY_OF_MONTH, endCal.getMaximum(Calendar.DAY_OF_MONTH));
		toDayLimite(endCal, false);
		endDate = endCal.getTime();
		
		performSearch();
	}
	
	private void toDayLimite(Calendar cal, boolean pol) {
		if(pol) {
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
		}else {
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
		}
	}
	
	private void lastXDays(Calendar cal, int days) {
		cal.add(Calendar.DAY_OF_MONTH, -days);
	}
	
	private Collection<QName> getTypes() {
		Collection<QName> types = new ArrayList<QName>();
		TypeDefinition typeDef;
		for (QName qName : dictionaryService.getAllTypes()) {
			typeDef = dictionaryService.getType(qName);
			if(typeDef != null && dictionaryService.isSubClass(typeDef.getName(), ContentModel.TYPE_CONTENT)) {
				//Por enquanto
				if(typeDef.getName().toString().indexOf(".model") != -1) {
					types.add(qName);
				}
			}
		}
		
		return types;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public NodeService getNodeService() {
		return nodeService;
	}

	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}

	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
}